package com.wen.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置Swagger的Bean实例Docket
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles of = Profiles.of("dev","test");
        //判读当前是否处于该种环境
        boolean flag = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("温帅")
                .enable(flag)//配置swagger是否启用，false则不启用，浏览器将无法访问
                .select()//通过.select()方法，去配置扫描接口，RequestHanderSeclectors配置如何扫描接口
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/hello/**"))//配置通过path过滤，即这里只扫描/hello开头的接口
                .build();

    }
    //配置多个分组，实际开发中不同的人会开发不同的接口，所以要分组
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Team1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Team2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Team3");
    }
    //可以通过apiinfo（）属性配置文档信息
    private ApiInfo apiInfo(){
        Contact contact = new Contact("温帅","14337878@qq.com","14337878@qq.com");
        return new ApiInfo("温帅的Swagger学习",
                "学习可太香了",
                "v1.0",
                "urn:tos",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}
